package com.good.child.http;

import android.content.Context;
import android.text.TextUtils;

import com.good.child.R;
import com.good.child.bean.BaseResponse;
import com.good.child.bean.login.LoginBean;
import com.good.child.bean.login.UserInfoBean;
import com.good.child.config.Api;
import com.good.child.config.Constants;
import com.good.child.config.RequestID;
import com.lzy.okgo.model.Response;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.http.BaseReqBean;
import com.yimidida.library.http.LoadingBaseObserver;
import com.yimidida.library.tool.JsonSerializer;
import com.yimidida.library.utils.LogUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create on: 2024/7/31
 * by Mr.Zhu
 * Description: TODO
 */
public class LoginLogical extends AppBaseLogical {

    private static LoginLogical instance;

    public static LoginLogical getInstance() {
        if (instance == null) {
            synchronized (LoginLogical.class) {
                if (instance == null) {
                    instance = new LoginLogical();
                }
            }
        }
        return instance;
    }

    /**
     * 请求验证码
     *
     * @param context
     * @param telephone
     * @param resultListener
     */
    public void requestVerifyCode(Context context,
                                  String telephone,
                                  ICallBackResultListener resultListener) {
        String url = Api.Login.URL_GET_VERIFY;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("telephone", telephone);
        requestStringForPostNew(url, reqBean.getReqMap(), new UserObserver(context, RequestID.Login.VERIFY_ID, resultListener));
    }

    /**
     * 请求登陆
     *
     * @param context
     * @param telephone
     * @param vercode
     * @param resultListener
     */
    public void requestUserLogin(Context context,
                                 String telephone, String vercode,
                                 ICallBackResultListener resultListener) {
        String url = Api.Login.URL_LOGIN;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("telephone", telephone);
        reqBean.getMap().put("vercode", vercode);
        reqBean.getMap().put("loginType", "1");
        requestStringForPostNew(url, reqBean.getReqMap(), new UserObserver(context, RequestID.Login.LOGIN_ID, resultListener));
    }

    public void getUserInfo(Context context, String deviceCode, ICallBackResultListener resultListener) {

        String url = Api.Login.URL_GET_USERINFO;
        Map<String, String> map = new HashMap<>();
        map.put("deviceCode", deviceCode);
        requestStringForGet(url, map, new UserObserver(context, RequestID.Login.GET_USERINFO, resultListener));

    }
    /**
     * 设置昵称
     * @param context
     * @param nikeName
     * @param sex
     * @param resultListener
     */
    public void requestSettingUserName(Context context,String nikeName,int sex,ICallBackResultListener resultListener) {
        String url = Api.Login.URL_UPDATE_USER_SEX;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("userName", nikeName);
        reqBean.getMap().put("sex", sex);
        reqBean.getMap().put("deviceCode", Constants.deviceCode);
        requestStringForPostNew(url, reqBean.getReqMap(), new UserObserver(context, RequestID.Login.UPDATE_NIKE_SEX, resultListener));
    }


    public void updateUserName(Context context, String deviceCode, String nikeName, ICallBackResultListener resultListener) {
        String url = Api.Login.URL_UPDATE_USER;
        updateUserInfo(url, null, deviceCode, nikeName, new UserObserver(context, RequestID.Login.UPDATE_NIKE, resultListener));
    }

    public void updateUserHeader(Context context, List<String> fileList, String deviceCode, ICallBackResultListener resultListener) {
        String url = Api.Login.URL_UPDATE_USER;
        updateUserInfo(url, fileList, deviceCode, null, new UserObserver(context, RequestID.Login.UPDATE_HEADER, resultListener));
    }
    /**
     * 请求登陆
     *
     * @param context
     * @param telephone
     * @param deviceCode
     * @param resultListener
     */
    public void requestAppAutoLogin(Context context,
                                    String telephone, String deviceCode,
                                    ICallBackResultListener resultListener) {
        String url = Api.Login.URL_APP_AUTO_LOGIN;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("telephone", telephone);
        reqBean.getMap().put("deviceCode", deviceCode);
        reqBean.getMap().put("loginType", "1");
        requestStringForPostNew(url, reqBean.getReqMap(), new UserObserver(context, RequestID.Login.LOGIN_ID, resultListener));
    }


    private class UserObserver extends LoadingBaseObserver {
        private ICallBackResultListener resultListener;
        private int requestID;

        public UserObserver(Context activity, int requestID, ICallBackResultListener resultListener) {
            this.resultListener = resultListener;
            this.requestID = requestID;
            setShowLoading(activity);
        }

        @Override
        public void onNext(Response<String> stringResponse) {
            super.onNext(stringResponse);
            if (stringResponse == null || stringResponse.code() != SUCCESS_CODE || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail);
                return;
            }
            String bodyString = stringResponse.body();
            try {
                switch (requestID) {
                    case RequestID.Login.LOGIN_ID:
                        LogUtils.showLog("账号登录 ：" + bodyString);
                        LoginBean bean = JsonSerializer.deSerialize(bodyString, LoginBean.class);
                        if (bean.isDataSuccess()) {
                            if (resultListener != null) {
                                resultListener.onCallBack(bean.data);
                            }
                        } else {
                            ToastUtil.showLong(bean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;
                    case RequestID.Login.GET_USERINFO:
                        LogUtils.showLog("获取用户信息 ：" + bodyString);
                        UserInfoBean bean1 = JsonSerializer.deSerialize(bodyString, UserInfoBean.class);
                        if (bean1.isDataSuccess()) {
                            if (resultListener != null) {
                                resultListener.onCallBack(bean1.data);
                            }
                        } else {
                            ToastUtil.showLong(bean1.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;
                    case RequestID.Login.UPDATE_NIKE:
                    case RequestID.Login.UPDATE_NIKE_SEX:
                        BaseResponse baseBean = JsonSerializer.deSerialize(bodyString, BaseResponse.class);
                        if (baseBean.code == 200) {
                            if (resultListener != null) {
                                resultListener.onCallBack(true);
                            }
                        } else {
                            ToastUtil.showLong(baseBean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(false);
                            }
                        }
                        break;
                    case RequestID.Login.UPDATE_HEADER:
                        UserInfoBean userInfoBean = JsonSerializer.deSerialize(bodyString, UserInfoBean.class);
                        if (userInfoBean.isDataSuccess()) {
                            if (resultListener != null) {
                                resultListener.onCallBack(userInfoBean);
                            }
                        } else {
                            ToastUtil.showLong(userInfoBean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;
                }
            } catch (Exception e) {
                ToastUtil.showLong(R.string.data_exception);
                if (resultListener != null) {
                    resultListener.onCallBack(0);
                }
            }

        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastUtil.showLong(R.string.data_error);
            if (resultListener != null) {
                resultListener.onCallBack(null);
            }
        }
    }
}
